/**
 * Machine Apps - All Rights Reserved.
 */
package com.machineapps.botesom.activity;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.machineapps.botesom.BoteSom;
import com.machineapps.botesom.R;
import com.machineapps.botesom.model.Category;
import com.machineapps.botesom.repository.CategoryRepository;
import com.machineapps.botesom.repository.CategoryRepositorySQLite;
import com.machineapps.framework.util.Messages;

/*
 *========== ==========================  ==============================================================================
 * Date      Author                      Comment
 *========== ==========================  ==============================================================================
 *07/11/2011 Romulo Quidute Filho        Initial Version.
 */
public class ManageCategory extends Activity implements OnClickListener {

	public static CategoryRepository repositoryCategory;

	private EditText categoryNameText;

	Context context;

	String categoryIcon;
	ImageView iconSelected;
	private Long id = 0L;
	private ImageView categoryImage;

	@Override
	protected void onCreate(Bundle icicle) {
		super.onCreate(icicle);

		repositoryCategory = new CategoryRepositorySQLite(this);

		setContentView(R.layout.form_manage_category);

		this.iconSelected = (ImageView) findViewById(R.id.category_image);

		Button confirmButton = (Button) findViewById(R.id.confirmarButton);
		this.categoryNameText = (EditText) findViewById(R.id.category_text_name);
		confirmButton.setOnClickListener(this);

		Button cancelButton = (Button) findViewById(R.id.cancelButton);
		cancelButton.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View view) {
				finish();
			}
		});

		modelToView();

		this.loadIcons();
	}

	/**
	 * Load the form according to model
	 */
	private void modelToView() {
		Bundle extras = getIntent().getExtras();

		// Check update mode
		if (extras != null) {
			id = extras.getLong(BoteSom.EXTRAS_CATEGORY);

			if (id != null) {
				Category category = repositoryCategory.get(id);
				categoryNameText.setText(category.getName());
				// Selects the icon according to the icon string
				selectIconFromString(category.getIcon());
				categoryIcon = category.getIcon();
			}
		}

	}

	private void selectIconFromString(String iconStr) {
		int resource = getResources().getIdentifier(iconStr, "drawable", "com.machineapps.botesom");
		iconSelected.setImageResource(resource);
	}

	public void loadIcons() {
		LinearLayout ll = (LinearLayout) findViewById(R.id.linearLayout1);
		ll.setPadding(10, 20, 0, 0);

		ImageView figura = new ImageView(this);
		figura = new ImageView(this);
		figura.setPadding(25, 10, 0, 0);
		figura.setTag("category_games");
		figura.setImageResource(R.drawable.category_games);
		figura.setOnClickListener(new SelectCategoryListener());

		ll.addView(figura);

		figura = new ImageView(this);
		figura.setPadding(25, 10, 0, 0);
		figura.setTag("category_popcorn");
		figura.setImageResource(R.drawable.category_popcorn);
		figura.setOnClickListener(new SelectCategoryListener());

		ll.addView(figura);

		figura = new ImageView(this);
		figura.setPadding(25, 10, 0, 0);
		figura.setTag("category_casual");
		figura.setImageResource(R.drawable.category_casual);
		figura.setOnClickListener(new SelectCategoryListener());

		ll.addView(figura);

		LinearLayout ll2 = (LinearLayout) findViewById(R.id.linearLayout2);
		ll2.setPadding(10, 20, 0, 0);
		figura = new ImageView(this);
		figura.setPadding(25, 10, 0, 0);
		figura.setTag("category_guitar");
		figura.setImageResource(R.drawable.category_guitar);
		figura.setOnClickListener(new SelectCategoryListener());

		ll2.addView(figura);

		figura = new ImageView(this);
		figura.setPadding(25, 10, 0, 0);
		figura.setTag("category_music");
		figura.setImageResource(R.drawable.category_music);
		figura.setOnClickListener(new SelectCategoryListener());

		ll2.addView(figura);

		figura = new ImageView(this);
		figura.setPadding(25, 10, 0, 0);
		figura.setTag("category_world");
		figura.setImageResource(R.drawable.category_world);
		figura.setOnClickListener(new SelectCategoryListener());

		ll2.addView(figura);

		LinearLayout ll3 = (LinearLayout) findViewById(R.id.linearLayout3);
		ll3.setPadding(10, 20, 0, 0);
		figura = new ImageView(this);
		figura.setPadding(25, 10, 0, 0);
		figura.setTag("category_nature");
		figura.setImageResource(R.drawable.category_nature);
		figura.setOnClickListener(new SelectCategoryListener());

		ll3.addView(figura);

		figura = new ImageView(this);
		figura.setPadding(25, 10, 0, 0);
		figura.setTag("category_ball");
		figura.setImageResource(R.drawable.category_ball);
		figura.setOnClickListener(new SelectCategoryListener());

		ll3.addView(figura);

		figura = new ImageView(this);
		figura.setPadding(25, 10, 0, 0);
		figura.setTag("category_music2");
		figura.setImageResource(R.drawable.category_music2);
		figura.setOnClickListener(new SelectCategoryListener());

		ll3.addView(figura);
	}

	class SelectCategoryListener implements OnClickListener {
		@Override
		public void onClick(View view) {
			ImageView iconView = (ImageView) view;
			categoryIcon = (String) iconView.getTag();
			iconSelected = (ImageView) findViewById(R.id.category_image);
			// Selects the icon according to the icon string
			selectIconFromString(categoryIcon);
		}
	}

	@Override
	public void onClick(View arg0) {
		if (categoryNameText.getText().toString().trim().equals("")) {
			Toast.makeText(ManageCategory.this, getString(R.string.category_name_required), 2500).show();
			return;
		}

		if (categoryIcon == null) {
			Toast.makeText(ManageCategory.this, getString(R.string.category_icon_required), 2500).show();
			return;
		}

		String message = "";

		Category category = new Category();
		// Check the id in order to check create/update
		if (id != null && id > 0) {
			category.setId(id);
			message = Messages.getFormattedString(getString(R.string.category_updated),
					new Object[] { categoryNameText.getText() });
		} else {
			message = Messages.getFormattedString(getString(R.string.category_created),
					new Object[] { categoryNameText.getText() });
		}

		category.setName(categoryNameText.getText().toString());
		category.setIcon(categoryIcon);

		// Adds or update a category
		repositoryCategory.createCategory(category);

		Toast.makeText(ManageCategory.this, message, Toast.LENGTH_SHORT).show();

		// Closes the activity
		this.finish();
	}

}
